<?php
/**
 * Application level View Helper
 *
 * This file is application-wide helper file. You can put all
 * application-wide helper-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.View.Helper
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Helper', 'View');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 */
class AppHelper extends Helper {
	public $helpers = array('Html', 'Js', 'AclLink', 'Time', 'Form');

	function urlPhotoByUsuario($usuario){
		$imageUrl = "files/usuario/photo/" . $usuario['Usuario']['photo_dir'] . "/" . $usuario['Usuario']['photo'];
		$absolutImageUrl = WWW_ROOT . $imageUrl;

		if (file_exists($absolutImageUrl) and $usuario['Usuario']['photo'] <> ''){
			return "/" . $imageUrl;
		}

		return 'photo_default.gif';
	}

	function urlPhotoByImage($image){
		$imageUrl = "files/image/attachment/" . $image['dir'] . "/" . $image['attachment'];
		$absolutImageUrl = WWW_ROOT . $imageUrl;

		if (file_exists($absolutImageUrl) and $image['attachment'] <> ''){
			return "/" . $imageUrl;
		}	
	}

	function linkByImage($image){
		$imageUrl = "files/image/attachment/" . $image['dir'] . "/" . $image['attachment'];
		$absolutImageUrl = WWW_ROOT . $imageUrl;
		$imageUrl = "/" . $imageUrl;

		if (!(file_exists($absolutImageUrl) and $image['attachment'] <> '')){
			return;
		}

		$id = $image["id"];
		if (empty($image["name"])) {
			$labelName = $image["attachment"];
		} else {
			$labelName = $image["name"];
		}

		$this->Js->JqueryEngine->jQueryObject = 'jQuery';

		echo "<span>";

		if ($this->AclLink->check('Attachments.delete')){
			echo $this->Html->link($this->Html->image('delete.png'), array('controller' => 'attachments', 'action' => 'delete', $id), array('id' => "delete_attachment_$id", 'escape' => false, 'title' => 'Deletar'));
		}

		echo "&nbsp;";
		echo $this->Html->link("<span class='blue'>$labelName</span>", $imageUrl, array('escape' => false, 'class' => 'fancybox', 'data-fancybox-group' => 'gallery', 'title' => $labelName));
		echo "</br>";
		echo "</span>";

		if ($this->AclLink->check('Attachments.delete')){
			$this->Js->get("#delete_attachment_$id")->event('click',"
					var _confirm = confirm('Você tem certeza?');
					if (!_confirm) {
						return false;
					}

					jQuery.ajax({url: jQuery(this).attr('href')});
					removeAttachment(this);

					return false;
			");
		}
	}

	function isPdf($attachment){
		return ($attachment["type"] == "application/pdf");
	}
}